package com.example.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.example.demo.mapper.TasksMapper;
import com.example.demo.pojo.entity.GenerateHistory;
import com.example.demo.pojo.entity.Task;
import com.example.demo.pojo.vo.TaskDetailVO;
import com.example.demo.service.TasksService;
import com.example.demo.utils.BaseContext;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TasksServiceImpl  implements TasksService {
    @Autowired
    private TasksMapper tasksMapper;
    @Override
    public void deleteAllTasks(Long id) {
        // 先删除任务关联的历史记录
        tasksMapper.deleteTaskHistories(id);
        // 再删除任务
        tasksMapper.deleteAllTasks(id);
    }

    @Override
    public TaskDetailVO getAllHistories_Tasks(Integer id) {
        TaskDetailVO taskDetailVO = new TaskDetailVO();
        tasksMapper.getAllHistories_Tasks(id);
        BeanUtil.copyProperties( tasksMapper.getTask(id), taskDetailVO);
        taskDetailVO.setGenerateHistoryList( tasksMapper.getAllHistories_Tasks(id));
        return taskDetailVO;
    }

    @Override
    public Page<Task> getAllTasks(Integer page, Integer size ) {
        PageHelper.startPage(page,size);
        Integer userId= Math.toIntExact(BaseContext.getCurrentId());
        List<Task> tasksList = tasksMapper.list(userId);
        Page<Task> p = (Page<Task>) tasksList;
        return p;
    }

    @Override
    public void createTask(Task task) {
        tasksMapper.createTask(task);
    }

    @Override
    public void deleteGenerateHistory(Long id) {
        tasksMapper.deleteGenerateHistory(id);
    }


}
